/**
 * Created by LeungZ on 2016/10/17.
 */
class Base {
    constructor(ctgNotification, appSrv,LesseeSrv, $scope, $state, clusterList) {
        "ngInject";
        $scope.$parent.vm.step = 1;
        this._appSrv = appSrv;
        this._LesseeSrv=LesseeSrv;
        this._notification = ctgNotification;
        this._fData = $scope.$parent.vm.fData;
        this._state = $state;

        if (clusterList) {
            this.clusterList = clusterList;
        }

        if(this._fData.hasOwnProperty('app')){
            this.name = this._fData.app.name;
            this.code = this._fData.app.code;
            this.desc = this._fData.app.desc;
            this.cluster = this._fData.cluster;
            this.lessee={
                namespaceCode:this._fData.namespaceCode
            };

            this.checkAppCode();
            this.getLessee();
        }
    }
 getLessee(){
     this.lesseeLoading=true;
        this._LesseeSrv.getRCList({page:1,perPage:999,code:this.cluster.clusterCode}).then((data)=>{
            this.lesseeList=data.list;
        }).catch(()=>{
            this._notification.error('获取租户列表数据有误');
        }).finally(()=>{
            this.lesseeLoading=false;
        })
       
    }
    checkAppCode() {
        if (this.code&&this.lessee) {
            this.appCodeChecking = true;
            this._appSrv.checkAppCode(this.code,this.lessee.namespaceCode).then(data=> {
                this.baseForm.code.$setValidity('duplicate',!data.exist);
                this.baseForm.code.$setValidity('network',true);
            }).catch(data=> {
                this.baseForm.code.$setValidity('network',false);
            }).finally(()=>{
                this.appCodeChecking = false;
            });
        }
    }

    next(){
        if(!this.appCodeChecking) {
            this._fData.app = {
                code: this.code,
                name: this.name,
                desc: this.desc
            };
            this._fData.cluster = this.cluster;
            this._fData.namespaceCode = this.lessee.namespaceCode;
            this._state.go('app-service-publish.deploy');
        }
    }
}
export default Base